﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Accumulation.Controls.Menus {
    internal class Helper {
        internal static string GetWebResourceUrl(string urlName, Control control) {
            return control.Page.ClientScript.GetWebResourceUrl(control.GetType(), urlName);
        }
        internal static string GetBool(bool value) {
            return value.ToString().ToLower();
        }
        internal static string GetClientJavaScriptCode(string code) {
            return "function(){" + code + "}";
        }
        internal static string ConstructMenuItems(StringBuilder sb, int i,
            MenuItemPlusCollection mpc, string jsObject, TargetEnum target) {
            const string item = "var {0} = {1}.Item('{2}', '{3}');";
            const string link = "{0}.Link('{1}', '{2}', {3}, '{4}', {5});";
            const string linkJsFunc = "{0}.Link('{1}', {2}, {3}, '{4}', {5});";
            const string line = "{0}.Line();";
            string itemObject = "{0}_{1}";
            foreach (MenuItemPlus mip in mpc) {
                if (mip.Visible == false) { continue; }
                itemObject = string.Format(itemObject, mip.Parent != null ? string.Empty : "item", i++);
                if (mip.Items != null && mip.Items.Count > 0) {
                    sb.AppendFormat(item, itemObject, jsObject, mip.Text, mip.ImageUrl);
                    if (mip.Items != null && mip.Items.Count > 0) {
                        Helper.ConstructMenuItems(sb, i, mip.Items, itemObject, target);
                    }
                }
                else {
                    if (!string.IsNullOrEmpty(mip.Text)) {
                        sb.AppendFormat(string.IsNullOrEmpty(mip.ClientClick) ? link : linkJsFunc,
                            jsObject, mip.Text, string.IsNullOrEmpty(mip.ClientClick) ? mip.NavigateUrl :
                            Helper.GetClientJavaScriptCode(mip.ClientClick),
                            Helper.GetBool(!mip.Enabled), mip.ImageUrl, mip.Target == target ? "null" :
                            Helper.GetBool(mip.Target == TargetEnum._BLANK));
                    }
                    else {
                        sb.AppendFormat(line, jsObject);
                    }
                }
            }

            return sb.ToString();
        }
    }

    #region Enum

    /// <summary>
    /// 输出菜单类型
    /// </summary>
    public enum OutputEnum {
        /// <summary>
        /// 水平
        /// </summary>
        Horizontal,
        /// <summary>
        /// 垂直
        /// </summary>
        Vertical,
        /// <summary>
        /// 右键
        /// </summary>
        ContextMenu,
        /// <summary>
        /// 自定义
        /// </summary>
        Customer
    }
    /// <summary>
    /// 菜单项链接打开类型
    /// </summary>
    public enum TargetEnum {
        /// <summary>
        /// 新窗体
        /// </summary>
        _BLANK,
        /// <summary>
        /// 当前窗体
        /// </summary>
        _SELF
    }

    #endregion

    #region CSS

    /// <summary>
    /// 鼠标经过颜色设置类
    /// </summary>
    [TypeConverter(typeof(ExpandableObjectConverter)), Serializable()]
    public sealed class OverStyle {
        /// <summary>
        /// 字体颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color FontColor { get; set; }
        /// <summary>
        /// 背景颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color BackgroundColor { get; set; }
    }
    /// <summary>
    /// 线条颜色设置类
    /// </summary>
    [TypeConverter(typeof(ExpandableObjectConverter)), Serializable()]
    public sealed class LineStyle {
        /// <summary>
        /// 背景颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color BackgroundColor { get; set; }
    }
    /// <summary>
    /// 文字设置类
    /// </summary>
    [TypeConverter(typeof(ExpandableObjectConverter)), Serializable()]
    public sealed class TextFontStyle {
        /// <summary>
        /// 字体颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color Color { get; set; }
        /// <summary>
        /// 字体名称
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(FontConverter.FontNameConverter)), DefaultValue((string)null), Editor(typeof(FontNameEditor), typeof(UITypeEditor))]
        public string Family { get; set; }
        /// <summary>
        /// 字体大小
        /// </summary>
        [NotifyParentProperty(true), DefaultValue(typeof(FontUnit), "")]
        public FontUnit Size { get; set; }
    }
    /// <summary>
    /// 图标颜色设置类
    /// </summary>
    [TypeConverter(typeof(ExpandableObjectConverter)), Serializable()]
    public sealed class IconStyle {
        /// <summary>
        /// 右边框颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color BorderRightColor { get; set; }
        /// <summary>
        /// 背景颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color BackgroundColor { get; set; }
    }
    /// <summary>
    /// 最外层容器设置类
    /// </summary>
    [TypeConverter(typeof(ExpandableObjectConverter)), Serializable()]
    public sealed class ContainerStyle {
        /// <summary>
        /// 边框颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color BorderColor { get; set; }
        /// <summary>
        /// 背景颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color BackgroundColor { get; set; }
        /// <summary>
        /// 宽度(px)
        /// </summary>
        [NotifyParentProperty(true), DefaultValue(typeof(Unit), "")]
        public Unit BorderWidth { get; set; }
    }
    /// <summary>
    /// 水平时最外层容器设置类
    /// </summary>
    [TypeConverter(typeof(ExpandableObjectConverter)), Serializable()]
    public sealed class HorizontalStyle {
        /// <summary>
        /// 边框颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color BorderColor { get; set; }
        /// <summary>
        /// 背景颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color BackgroundColor { get; set; }
        /// <summary>
        /// 线条颜色
        /// </summary>
        [NotifyParentProperty(true), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
        public Color LineColor { get; set; }
    }

    #endregion
}